﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;


namespace MPMusicVids.Log
{
    public class Logging
    {
        static Logging instance = null;
        StreamWriter writer;
        bool _init = false;
        static readonly object padlock = new object();
        private string prePend = "";

        private Logging()
        {
        }

        public static Logging getLogger()
        {
            lock (padlock)
            {
                if (instance == null)
                    instance = new Logging();
                return instance;
            }
        }

        public enum LogLevel{
            Info,
            Warning,
            Exception
        }

        private void init()
        {
            string fileName = MediaPortal.Configuration.Config.GetFile(MediaPortal.Configuration.Config.Dir.Log, "MusicVids.log");
            writer = new StreamWriter(fileName, true);
            _init = true;
        }

        public void Append(string logText)
        {
            Append(logText, LogLevel.Info);
        }

        public void Append(string logText, LogLevel logType)
        {
            lock (padlock)
            {
                init();
                prePend = DateTime.Now.ToLongTimeString() + ": ";
                string midPend = "";
                switch (logType)
                {
                    case LogLevel.Exception:
                        midPend = "FATAL ERROR: ";
                        break;
                    case LogLevel.Warning:
                        midPend = "WARNING: ";
                        break;
                    case LogLevel.Info:
                        midPend = "Info: ";
                        break;
                }
                writer.WriteLine(prePend + midPend + logText);
                writer.Close();
                _init = false;
            }
        }

    }
}

